package com.cph.common.http;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author zhuqa
 * @projectName vehicle-files-management
 * @description: TODO
 * @date 2019/9/2 15:03
 */
public class BeanMapUtils
{

    private static Logger logger = LoggerFactory.getLogger(BeanMapUtils.class);

    /**
     * 将bean对象转成map
     *
     * @param o 对象参数
     * @return Map<String, Object>
     * @throws
     * @author:willinggod
     * @method retryHttpGet
     */
    public static Map<String, Object> BeanToMap(Object o) throws
            IntrospectionException,
            IllegalAccessException,
            InvocationTargetException
    {
        if (o == null)
        {
            return null;
        }
        else
        {
            Map<String, Object> returnMap = new HashMap<String, Object>();
            try
            {
                Class t = o.getClass();
                BeanInfo beanInfo = Introspector.getBeanInfo(t);
                PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
                for (int i = 0; i < propertyDescriptors.length; i++)
                {
                    PropertyDescriptor descriptor = propertyDescriptors[i];
                    String propertyName = descriptor.getName();
                    if (!"class".equals(propertyName))
                    {
                        Method readMethod = descriptor.getReadMethod();
                        Object result = readMethod.invoke(o, new Object[0]);
                        if (null != result)
                        {
                            returnMap.put(propertyName, result);
                        }
                        else
                        {
                            returnMap.put(propertyName, "");
                        }
                    }
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
                logger.info("BeanToMap error" + e.getMessage());
            }
            finally
            {

            }

            return returnMap;
        }
    }

    /**
     * 判断map对象中的key对应的value是否为空或者null
     *
     * @param map map对象
     * @param key 需要判断的key值
     * @return Map 转换后的对象
     * @throws Exception
     * @author willinggod
     */
    public static boolean isMapValueBlank(Map<String, Object> map, String key)
    {
        boolean isMapValueNull = false;
        for (Map.Entry<String, Object> item : map.entrySet())
        {
            if (null != item.getKey())
            {
                String keyName = item.getKey();
                if (keyName.equals(key))
                {
                    Object value = item.getValue();
                    if (null == value || StringUtils.isBlank(value.toString()))
                    {
                        isMapValueNull = true;
                        break;
                    }
                }
            }
        }
        return isMapValueNull;
    }

    /**
     * 给对象的BigDecimal属性赋默认值 0
     *
     * @param o
     * @param defaultVal
     * @author wangjingchuan
     * @since 2018年5月15日18:06:40
     */
    public static void setObjectBigDecimalDefaultValue(Object o, BigDecimal... defaultVal)
    {
        if (o != null)
        {
            BigDecimal df = new BigDecimal("0");
            if (null != defaultVal && defaultVal.length > 0)
            {
                df = defaultVal[0];
            }
            Method[] methods = o.getClass().getMethods();
            if (null == methods)
            {
                return;
            }
            for (Method method : methods)
            {
                String methodName = method.getName();
                if (methodName.startsWith("get") && BigDecimal.class.getName().equals(method.getReturnType().getName()))
                {
                    try
                    {
                        if (null == method.invoke(o))
                        {
                            o.getClass().getMethod(methodName.replace("get", "set"), BigDecimal.class).invoke(o, df);
                        }
                    }
                    catch (Exception e)
                    {
                        logger.error(e.getMessage(), e);
                    }
                }
            }
        }
    }


}
